package com.naire.modul.employee.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.common.dao.GeneralDaoImpl;
import net.common.service.PageBean;
import net.common.util.BeanUtils;

import com.naire.modul.dept.pojo.Dept;
import com.naire.modul.employee.dao.EmployeeDao;
import com.naire.modul.employee.pojo.Employee;
import com.naire.modul.employee.vo.EmployeeVo;

public class EmployeeDaoImpl extends GeneralDaoImpl implements EmployeeDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<EmployeeVo> search(PageBean pageBean, Integer dept_id) throws Exception {
		StringBuilder hql = new StringBuilder("from Employee where flag=0 and dept.dept_id=:dept_id");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("dept_id", dept_id);
		List<EmployeeVo> d = new ArrayList<EmployeeVo>();
		List<Employee> l = hibernateHandler.query(hql, pageBean, params);
		for(Employee bean : l) {
			EmployeeVo dv = new EmployeeVo();
			BeanUtils.copyProperties(bean, dv);
			dv.setDept_name(bean.getDept().getDept_name());
			d.add(dv);
		}
		return d;
	}

	@Override
	public void insert(EmployeeVo bean) throws Exception {
		Employee pojo = new Employee();
		BeanUtils.copyProperties(bean, pojo);
		// 部门
		Dept dept = (Dept) hibernateHandler.find(Dept.class, bean.getDept_id());
		pojo.setDept(dept);
		hibernateHandler.insert(pojo);
	}

	@Override
	public void update(EmployeeVo bean) throws Exception {
		Employee pojo = (Employee)hibernateHandler.find(Employee.class, bean.getEmpl_id());
		BeanUtils.copyProperties(bean, pojo);
		hibernateHandler.update(pojo);
	}

	@Override
	public void updateFlag(EmployeeVo bean) throws Exception {
		Employee pojo = (Employee)hibernateHandler.find(Employee.class, bean.getEmpl_id());
		BeanUtils.copyProperties(bean, pojo);
		hibernateHandler.update(pojo);
	}

	@Override
	public EmployeeVo find(Integer id) throws Exception {
		Employee pojo = (Employee)hibernateHandler.find(Employee.class, id);
		EmployeeVo bean = new EmployeeVo();
		BeanUtils.copyProperties(pojo, bean);
		return bean;
	}

}
